package bean;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.RenderableOp;
import javax.media.jai.RenderedOp;
import javax.servlet.http.HttpServletRequest;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;






public class MemberDTO {
	
	private Connection conn;
	private MemberVo vo;
	private String mid;
	private String dir="./WebContent/memberphoto/";
	
	
	
	public MemberDTO(){
		conn = new MyOracle().getConn();
	}
		
	
	public String getDir() {
		return dir;
	}


	public void setDir(String dir) {
		this.dir = dir;
	}


	public String getMid() {
		return mid;
	}


	public void setMid(String mid) {
		this.mid = mid;
	}


	public MemberVo getVo() {
		return vo;
	}

	public void setVo(MemberVo vo) {
		this.vo = vo;
	}
	public boolean insert(HttpServletRequest request){
		boolean b = false;
		String sql ="insert into member(mid,pwd,irum,nicname,birth,gender,email,phone,photo) "
				+ "values(?,?,?,?,?,?,?,?,?)";
			try{
				PreparedStatement pst = conn.prepareStatement(sql);
								
				int size = 10*1024*1024;
//				String savedir = "F:/java work space/web_1406/WebContent/memberphoto/";
				String savedir = "C:/fserv/member/";
//				String savedir = "C:/jamdol/javaWorkspace/exam/WebContent/sm_image/";
				String orifile="";
				String sysfile="";
				MultipartRequest multi = new MultipartRequest(request,savedir,size,"utf-8",new DefaultFileRenamePolicy());
				Enumeration file = multi.getFileNames();
				String tempfile = (String)file.nextElement();
				orifile = multi.getOriginalFileName(tempfile);
				sysfile = multi.getFilesystemName(tempfile);
				
				
				ParameterBlock pd = new ParameterBlock();
				pd.add(savedir+sysfile);
				RenderedOp rop = JAI.create("fileload", pd);
				BufferedImage bi = rop.getAsBufferedImage();
				BufferedImage thumb = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
				Graphics2D g = thumb.createGraphics();
				g.drawImage(bi, 0, 0, 100, 100, null);
				savedir = "C:/jamdol/javaWorkspace/exam/WebContent/sm_image/";
				File ff = new File(savedir + "sm_" + sysfile);
				ImageIO.write(thumb, "jpg", ff);
				
				pst.setString(1, multi.getParameter("mid"));
				pst.setString(2, multi.getParameter("pwd"));
				pst.setString(3, multi.getParameter("irum"));
				pst.setString(4, multi.getParameter("nickname"));
				pst.setString(5, multi.getParameter("birth"));
				pst.setString(6, multi.getParameter("gender"));
				pst.setString(7, multi.getParameter("email"));
				pst.setString(8, multi.getParameter("phone"));
				pst.setString(9, sysfile);
				this.mid = multi.getParameter("mid");
				
				
				
				
				int n = pst.executeUpdate();
				if(n>0)
				{
					b=true;
				}
				
				
				
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
		return b;
	}


	public boolean login(){
		boolean b = false;
		try{
			
			String sql = "select mid,pwd from member where mid=? and pwd=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			pst.setString(2, vo.getPwd());
			
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()){
				b=true;
			}
			pst.close();
			conn.close();
				
		}catch(Exception e){
			e.printStackTrace();
			
		}
		return b;
		
	}
	public ArrayList<MemberVo> select(){
		ArrayList<MemberVo> ar = new ArrayList<MemberVo>();
		String sql ="select * from member";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			
			ResultSet rs = pst.executeQuery();
			
			while(rs.next()){
				MemberVo v = new MemberVo();
				v.setMid(rs.getString("mid"));
				v.setIrum(rs.getString("irum"));
				v.setBirth(rs.getString("birth"));
				v.setEmail(rs.getString("email"));
				v.setGender(rs.getString("gender"));
				v.setNickname(rs.getString("nicname"));
				v.setPhone(rs.getString("phone"));
				v.setPhoto(rs.getString("photo"));
								
				ar.add(v);
			}
					
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return ar;
	}
	public MemberVo selectone(){
		MemberVo v = new MemberVo();
		String sql ="select * from member where mid=?";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()){
				v.setMid(rs.getString("mid"));
				v.setIrum(rs.getString("irum"));
				v.setBirth(rs.getString("birth"));
				v.setEmail(rs.getString("email"));
				v.setGender(rs.getString("gender"));
				v.setNickname(rs.getString("nicname"));
				v.setPhone(rs.getString("phone"));
				v.setPhoto(rs.getString("photo"));
				v.setPwd(rs.getString("pwd"));
			}
					
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return v;
	}
	
	public boolean delete(){
		boolean b = false;
		String sql = "delete from member where mid = ?";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			int n = pst.executeUpdate();
			
			if(n>0){
				b=true;
//				String dir = "F:/java work space/web_1406/WebContent/memberphoto/";
				String dir = "C:/fserv/member";
				File f = new File(dir+vo.getPhoto());
				if(f.exists()){
					f.delete();
				}
				
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}
		return b;
	}
	
	
	
}
